using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace Business.Entities
{
    [Serializable]
    public class ErrorPackage14
    {
        /// <summary>
        /// Debug data
        /// </summary>
        public int LastCheckPoint = -1;
        public string OutputLog;
        public string Version;
        public List<HttpLog> HttpLogs;
        public string ExceptionStackTrace;
        public string ExceptionMessage;

        public void SetException(Exception ex)
        {
            ExceptionStackTrace = ex.StackTrace;
            ExceptionMessage = ex.Message;
        }

        public ErrorPackage14()
        {
            Reset();
            try
            {
                Version = "N/A (100)";
                Version = Context.GetAssemblyVersion();
            }
            catch (Exception)
            {
            }
        }

        public string SerializeToString()
        {
            StringBuilder sb = new StringBuilder();
            using (StringWriter sw = new StringWriter(sb))
            {
                var xs = new XmlSerializer(typeof (ErrorPackage));
                xs.Serialize(sw, this);
                sw.Close();
            }
            return sb.ToString();
        }

        internal void Reset()
        {
            LastCheckPoint = -1;
            HttpLogs = new List<HttpLog>();
        }
        
        public void AddHttpLog(HttpLog log)
        {
            HttpLogs.Add(log);
        }

        public HttpLog AddNewHttpLog(string url, int checkPoint)
        {
            HttpLog log = new HttpLog(url, checkPoint);
            HttpLogs.Add(log);
            return log;
        }

        public HttpLog AddNewHttpLog(string url, CookieCollection cookiesIn, int checkPoint)
        {
            HttpLog log = new HttpLog(url, checkPoint);
            log.CookiesIn = cookiesIn.CloneShallow();
            HttpLogs.Add(log);
            return log;
        }
    }
}
